<?php
/**
 * Created by PhpStorm.
 * User: ownfi
 * Date: 2017-04-11
 * Time: 20:00
 */

namespace App\Services\Impl;


use App\Common\Constant\Params;
use App\Models\Merchant;
use App\Models\Order;
use App\Models\OrderComplain;
use App\Models\OrderDetail;
use App\Models\OrderRefund;
use App\Models\OrderStatus;
use App\Services\IOrderService;
use Illuminate\Support\Collection;

class OrderServiceImpl implements IOrderService
{
    public function all(array $wheres = [])
    {
        $model = Order::alias('t1')->select('t1.*')->orderBy('t1.add_time','DESC')->with(['merchant','detail','status']);
        if ($wheres){
            $OrderStatus_wheres = [];
            foreach ($wheres as $where){
                if ($where[0] == 'me_username'){
                    $model->join(Merchant::table().' as m', function ($join) use ($where){
                        $join->on('t1.merchant_id', '=', 'm.me_id')
                            ->where([$where]);
                    });
                }else if ($where[0] == 'customer_address'){
                    $model->join(OrderDetail::table().' as d', function ($join) use ($where){
                        $join->on('t1.id', '=', 'd.order_id')
                            ->where(function($query) use ($where){
                                $query->where([$where])->orWhere('customer_area',$where[1],$where[2]);
                            });
                    });
                }else if ($where[0] == 'merchant_status'){
                    $OrderStatus_wheres[] = $where;
                }else if ($where[0] == 'except_status'){
                    $OrderStatus_wheres[] = $where;
                }else{
                    $model->where('t1.'.$where[0],$where[1],$where[2]);
                }
            }
            if (!empty($OrderStatus_wheres)){
                $model->join(OrderStatus::table().' as s', function ($join) use ($OrderStatus_wheres){
                    $join->on('t1.id', '=', 's.order_id')
                        ->where($OrderStatus_wheres);
                });
            }
        }
        return $model->paginate(config('common.perpage'));
    }

    public function arbitrateList(array $wheres = [])
    {
        $orderIds = OrderStatus::where('arbitrate_status',Params::ORDER_ARBITRATE_ARBITRATING)->select('order_id')->get()->pluck('order_id');
        if ($orderIds->isEmpty()){
            return Collection::make([]);
        }

        $model = Order::alias('t1')->whereIn("t1.id",$orderIds)->select('t1.*')->orderBy('t1.add_time','DESC')->with(['merchant','detail','status','refunds']);
        if ($wheres){
            foreach ($wheres as $where){
                if ($where[0] == 'me_username'){
                    $model->join(Merchant::table().' as m', function ($join) use ($where){
                        $join->on('t1.merchant_id', '=', 'm.me_id')
                            ->where([$where]);
                    });
                }else if ($where[0] == 'customer_address'){
                    $model->join(OrderDetail::table().' as d', function ($join) use ($where){
                        $join->on('t1.id', '=', 'd.order_id')
                            ->where([$where]);
                    });
                }else if ($where[0] == 'arbitrate_time'){
                    $model->join(OrderRefund::table().' as r', function ($join) use ($where){
                        $join->on('t1.id', '=', 'r.order_id')
                            ->where([$where]);
                    });
                }else{
                    $model->where('t1.'.$where[0],$where[1],$where[2]);
                }
            }
        }
        return $model->paginate(config('common.perpage'));
    }

    public function complainList(array $wheres = [])
    {
        $orderIds = OrderComplain::where('oc_handle_status',1)->select('oc_orderid')->get()->pluck('oc_orderid');
        if ($orderIds->isEmpty()){
            return Collection::make([]);
        }

        $model = Order::alias('t1')->whereIn("t1.id",$orderIds)->select('t1.*')->orderBy('t1.add_time','DESC')->with(['merchant','detail','status']);
        if ($wheres){
            $OrderStatus_wheres = $OrderComplain_wheres = [];
            foreach ($wheres as $where){
                if ($where[0] == 'me_username'){
                    $model->join(Merchant::table().' as m', function ($join) use ($where){
                        $join->on('t1.merchant_id', '=', 'm.me_id')
                            ->where([$where]);
                    });
                }else if ($where[0] == 'customer_address'){
                    $model->join(OrderDetail::table().' as d', function ($join) use ($where){
                        $join->on('t1.id', '=', 'd.order_id')
                            ->where([$where]);
                    });
                }else if ($where[0] == 'merchant_status'){
                    $OrderStatus_wheres[] = $where;
                }else if ($where[0] == 'except_status'){
                    $OrderStatus_wheres[] = $where;
                }else if ($where[0] == 'except_status'){
                    $OrderStatus_wheres[] = $where;
                }else if ($where[0] == 'oc_add_time'){
                    $OrderComplain_wheres[] = $where;
                }else{
                    $model->where('t1.'.$where[0],$where[1],$where[2]);
                }
            }
            if (!empty($OrderStatus_wheres)){
                $model->join(OrderStatus::table().' as s', function ($join) use ($OrderStatus_wheres){
                    $join->on('t1.id', '=', 's.order_id')
                        ->where($OrderStatus_wheres);
                });
            }
            if (!empty($OrderComplain_wheres)){
                $model->join(OrderComplain::table().' as c', function ($join) use ($OrderComplain_wheres){
                    $join->on('t1.id', '=', 'c.oc_orderid')
                        ->where($OrderComplain_wheres);
                });
            }
        }
        return $model->paginate(config('common.perpage'));
    }

    public function loadOrderAllInfo(Order $order)
    {
        $order->load(['detail','status','goods','merchant','master','evaluate','offers','offers.master','offers.master.wallet']);
    }

    public function cancel($order_id)
    {
        return OrderStatus::where('order_id',$order_id)->take(1)->update(['except_status'=>Params::ORDER_EXCEPT_CANCELED]);
    }
}